by Joe Carpenter
This chapter is intended to help you install through the Java, installation and give you a basic introduction to the Java Developer's Kit, as well asand give you several Java -enabled browsers. By the end of the chapter, you will have installed what you need to get going, and you'll have compiled and run your first Java application.
The Java Developer's Kit (JDK) is a software package that Sun has made available free to the public for free. thatThis package gives you all of the tools you need to start writing and running Java programs. It includes all of the basic components that make up the Java environment, including the Java Compiler, the Java interpreter, an applet viewer that lets you see applets without opening a Java compatible wWeb browser, as well as a number of other programs that are useful in creating Java programs (all of which are explained in more detail in Chapter 4). The JDK represents the bare minimum of what you need to work with Java.
Sun's Java Developer's Kit is covered in more detail in Chapter 4 (JDK Tools)
If there's no such thing as a free lunch, then the JDK is more of a free light snack. Although it does contain all of the tools you really need to work with Java, it isn't an integrated development environment that many programmers are used to working with. The tools that come with the JDK are command-line driven; they don't have a nice graphical user interface like those of Visual C++ or Borland C++. The tools are intended to be called from the command prompt (the DOS prompt, for Windows 95 and NT systems). The files that contain your source code are plain ASCII text files that you create with a text editor (which you need to supply), such as the NotePad (for Win32 systems), vi (on UNIX), or BBEdit (on the Macintosh).
A C++ compiler takes high-level C++ code and compiles it into instructions that a computer's microprocessor can understand. This means that every different type of computer will need a different compiling of a given C++ program in order to be able to run it. Taking a C++ program and compiling it on different types of computers is not an easy task. Because different computers do things in different ways, the C++ program has to be able to handle those differences. This is a significant problem when dealing with the wide variety of computers available today.
The Java environment overcomes this problem by putting a middleman between the compiler and the computer. The middleman is called the Java Virtual Machine (JVM). Instead of compiling directly for one type of computer, the Java compiler, javac, takes the high-level, human-readable Java source code in a text file and compiles it into lower-level Java bytecodes that the JVM understands. The JVM then takes that bytecode and interprets it so that the Java program runs on the computer the JVM is running on. The only platform-specific program is the JVM itself. Similarly, Web browsers that support Java applets all have JVMs built into them.
The Java Virtual Machine concept provides a number of advantages-the main one being cross platform compatibility. Java programmers don't need to worry about how a computer platform handles specific tasks, and they don't need to worry about how to compile different versions of their program to run on different platforms. The only platform that programmers need to worry about is the JVM. Programmers can be reasonably confident that their program will run on whichever platforms have JVMs, such as Windows 95, Solaris, and Macintosh.
Even with Java, there are slight differences between platforms. When working with Java, it's a good idea to test a program on as many different types of computers as possible.
The main disadvantage of this system is that interpreting code is much slower than running a program that is native to the host computer. For each instruction in the Java bytecode, the JVM must figure out what the equivalent is for the system it is running on. This creates a slowdown in processing a Java program.
To overcome the speed limitation of Java, a number of Just-In-Time compilers (JITs) are available. JITs make the Java system even more confusing, but they make it run much faster, by taking the already compiled Java bytecode and compiling it into instructions that are native to a given computer. It's all done transparently to the user from within the JVM. The JIT, because it's part of the JVM, is also platform specific but will run any Java bytecode, regardless of what type of machine it came from. Using a JIT, a Java program can achieve speeds close to that of a native C++ program.
So now that you know a little bit more about what Java and the JDK are, you're now ready to get going on actually installing and usinge it.
If you haven't done so already,sit down at your computer, turn it on, and load up the cd-rom from the back of the book. On the CD-ROM, there is a directory called JDK. Inside the directory, "JDK" there are three subdirectories: MACINTOSH, SOLARIS, and WINDOWS. Each of these subdirectories contains the complete installation of Sun's Java Developer's Kit, for each of those three platforms. Table 3.1 shows what those refer to.
Table 3.1 Contents of the JDK folder on the CD-ROM
Directory | Contents |
MACINTOSH | Contains the JDK for the Macintosh platform, both 68k and PowerPC. |
SOLARIS | Contains two subdirectories, one for the SPARC Solaris JDK and one for the x86 Solaris JDK |
WINDOWS | Contains the JDK for x86 32 bit Windows systems, namely Windows 95 and Windows NT. |
Alternately, you can use a Web browser and a connection to the Internet to go get the JDK. If you are going to download the JDK, see the section "Downloading the JDK" later in this chapter.
What if you're not using one of those three platforms? You may or may not be in luck. There are a number of other JDKs for other platforms, but you may need to look around the Internet for them. The three previous ones are supported by Sun-any other platforms are not. There are ports for systems such as Linux, DEC Alpha, Amiga, and many others. The best place to look for information on those releases is at the Java External Related Mailing Lists and Resources page:
Now we'll look at how to install the JDK onto 32 bit Windows systems from the CD-ROM. The setup is fairly easy, but you should be familiar with the Windows and DOS enviroments before attempting to install the JDK.
There should not be any. No copies of previous versions of the Java Developers Kit should be on your computer. If you have stored any additional Java source code files (files that you have written or files you have received from someone else) in a directory under the main JDK Java directory, you should move those files to a new directory before deleting previous versions of the JDK. You can delete the entire Java directory tree using
After removing the previous version of the JDK, execute the self-extracting archive to unpack the JDK files. You should unpack the file in the root directory of the C drive to create C:\JAVA. If you want the JDK in some other directory, unpack the archive file in that directory. Unpacking the archive creates a Java parent directory and all of the necessary subdirectories for this release.
Unpacking the archive also creates SRC.ZIP and LIB/CLASSES.ZIP. DO NOT UNZIP THE CLASSES.ZIP FILE. If you want to review the source for some of the JDK class libraries, you may unzip the SRC.ZIP file. However, you must use an unzip program that maintains long file names to unzip SRC.ZIP. Two such unzip utility programs are UnZip 5.12, which can be found at the UUNet FTP Ssite, and WinZip. Look for the file UNZ512XN.EXE file or a later version. The sites these can be found at are in table 3.2.
Do not unzip the classes.zip file! It contains all of the classes that the JDK needs to compile your code, such as the Java.lang and AWT classes. If you unzip it, the Java compiler may not be able to find the classes it needs, and will be unable to complete a compile.
After unpacking, you should add the JAVA\BIN directory onto the path. The easiest way to accomplish this is to edit the AUTOEXEC.BAT file and make the change to the path statement there.
If you have set the CLASSPATH environment variable, you may need to update it. You must replace CLASSPATH entries that pointed to the JAVA\CLASSES directory to point to JAVA\LIB\CLASSES.ZIP. Again, the easiest way to accomplish this is to edit the AUTOEXEC.BAT file and make the change to the CLASSPATH environment variable there.
After completing these changes to AUTOEXEC.BAT, save the file and reboot so that the changes take effect.
This section covers the installation of the JDK for x86 and SPARC Solaris UNIX Systems. This installation procedure is similar to some of the other UNIX operating system installations. For more information about geting ports of the JDK for other UNIX systems (such as Linux) see c
Copy the appropriate directory (either the x86 or Sparc Solaris release directory) onto your hard drive. Depending on how your file system is configured and the privileges on your system, you might want to either copy itthe directory into a public area, such as /USR/LOCAL, or into your home directory. The command to copy the Sparc release from the Solaris directory on the CD-ROM to your home directory is
The CLASSPATH enviroment variable is covered in more detail in chapter 4 (JDK Tools)
The CLASSPATH variable is an enviroment variable that defines a path to the "classes.zip" file. Most of the tools that come with the JDK use the CLASSPATH variable to find that file, so having it set correctly is fairly important. You can set the CLASSPATH variable at the command prompt by entering the following:
% setenv CLASSPATH .:/usr/local/java/lib/classes.zip
Or you can put this line of text in your .login or .profile files, so it's called every time you log in:
setenv CLASSPATH .:/usr/local/java/lib/classes.zip
Downloading the JDK
You can download the JDK off of the Internet instead of getting it from the CD-ROM in the back of the book. When you download the JDK off the Internet, you can be fairly certain that you're getting the latest version of drive. Either way, the JDK isit's not all that hard to download, but I still think using the CD-ROM is easier.it.
The first item you need to download the JDK is a computer with a connection to the Internet that can use a Web browser. The particular Web browser doesn't really matter all that much, but for these examples, I am using the Netscape Navigator browser.
The second item you need is some (well, actually, quite a bit) of free hard disk space on the machine to which you are planning on downloading the JDK to. Table 3.3 contains the amounts of disk space you need to download and decompress the JDK for each platform.
Table 3.3 Disk Space requirements for the JDK
Platform | Disk Space Compressed | Disk Space Uncompressed |
Solaris | 5.4 Meg | 10 Meg |
Windows | 4.4 Meg | 6.5 Meg |
Macintosh | 2.5 Meg | 6 Meg |
If you have some free disk space and a browser handy, you're ready to download. Let's get started!
The JDK is a pretty big file, and downloading is going to take a while. How long it takes depends on how fast your connection is, the user load on the ftp server at that particular moment, the network load on the Internet at the time of day you are downloading the file, the beating of a butterfly's wings somewhere on the planet, sunspots, blind luck, and a large number of other factors that are even more difficult to predict. If the file transfer is going too slow for your taste, try connecting at another time. Depending on where you are on the planet, good times to connect will vary, again depending on many of the same factors that control the transfer rate.
Installing a Downloaded JDK
Solaris x86 and SPARC platforms.
For Solaris, the JDK is normally distributed as a compressed-tape archive (a file with a TAR.Z extension); the name of the file indicates its version.
Use tar or your standard system backup process to back up any previous releases of the JDK before beginning installation of a new version!
You don't want to lose all that work you put into it, and you'll have a copy of the previous release in the event something goes wrong with your new copy.
Installing the JDK on a Solaris machine can be done one of two ways. It can either be installed into a user's home directory for individual use, or it can be installed into a public bin directory, such as /usr/local/bin/, so that all users on a system can use it. The installation process is the same for both.
drwxr-xr-x root other 512 Feb 18 21:34 /usr
Rather than set these variables from the command line each time, you probably should add the commands to set the PATH and CLASSPATH variables in your shell resource file-.shrc, .cshrc, .profile, and so on. If you are a system administrator installing the JDK as a network development tool, you may want to add these parameters to the default configuration files.
Windows Installation
You need Windows 95 or Windows NT to run Java. There is curently no JDK for Windows 3.1, 3.11 or Windows for Workgroups.
Why can't Java work under Windows 3.x?
At the time of this writing, no one has had the time, much less the patience, to produce a production-quality version of Java for earlier versions of Windows. There are several reasons for this, but the biggest ones seem to be these:
Installing the JDK is a fairly simple procedure, but you should know your way around the Windows and DOS enviroments. For Windows, the JDK is normally distributed as a self-extracting compressed file; the name of the file indicates its version.
Choose a directory for the installation. These instructions assume an installation location of C:\JAVA. If you choose a different base directory, simply append the appropriate path (and change the drive letter, if appropriate). For example, if you want to install to E:\TOOLS\JAVA, replace C: with e:\tools whenever it shows up in the instructions.
Do not install the JDK over a previous release, especially if the previous release is one of the pre-beta or beta 1 versions!
Rename the JAVA directory (for example, to OLDJAVA) using the Explorer in Windows 95 orWindows NT. If the installation fails for any reason, you can restore the previous version directly from oldjava. Otherwise, after the installation is complete, you can move any additional files, such as documentation, from your old installation into your new installation before removing it from your system.
For Windows NT 4.0 and later, you can skip steps 6, 7, and 8, and set the CLASSPATH from a properties sheet. You do not need to reboot, but you may have to close any DOS Prompt windows that you had open to use the new variable.
If you want to squeeze a little more performance out of Java (at the cost of a little more disk space), move the classes.zip file to C:\JAVA\CLASSES (you need to create this directory), and use PKUNZIP to extract the files. You can then remove the classes.zip file.
If you choose this option, you need to change your
For Macintosh, the JDK is normally distributed as a stuffed, bin-hexed archive (a file with a HQX.SIT extension). The file version is indicated in its name.
Make sure to archive your current version of the JDK before installing a newer version.
You don't want to lose all that work you put into it, and you'll have a copy of the previous release in the event something goes wrong with your new copy.
After following the previous instructions for downloading the MacJDK, you should have an installer titled MacJDK1.02.SEA. Double-click this installer so that it launches into a fairly standard Macintosh installer dialog box.
The Macintosh allows you to name directories and files in a manner that chokeThisof folder UNIX. Filenames that UNIX can't handle include the naming of directories with slashes (/). This causes problems with the JDK because it uses a mixed UNIX/Mac method of tracking paths when the JDK attempts to locate your files. Thus, a slash in the name of a directory is interpreted as a change of directory.
UNIX also has a few problems with names that include spaces. As of this release, you should follow the UNIX file and directory naming conventions used by the developers. That means you shouldn't use spaces, slashes, asterisks, and most other punctuation characters in your file and directory names. You can, however, use as many periods as you want, and the file name can be as long as you want it (as long as it's less than 32 characters long).
For example, the following is a perfectly good Macintosh file name, but will not work under UNIX:
/../..../Stuff \/\/..java
To work under UNIX and the Mac, the file name should look like this:
Stuff.java
In the lower-left hand corner of the installer dialog box in the Install Location area, you can specify where you want to install the JDK. After selecting the appropriate drive and directory, click the Install or hit "return" button to run the installer. It will put all of the Mac JDK in a directory called MACJDK at whatever location you specified in the installer. The default installation location is the root level of your startup disk.
You now have a working copy of the JDK on your hard drive folder. This includes two essential programs: the Java compiler and the Appletviewer. You are now ready to move onto the next (and much more fun) parts of Java development.
Now you're ready to writea small Java application to test your installation of the JDK.
Somewhere on your hard drive, create a new directory to store your projects. I call mine PROJECTS and I keep it out of the JDK directory, so that I don't need to move it around whenever I install a new version of the JDK. Inside that directory, create another directory called HELLOWORLD.
Now, using your favorite text editor (such as the NotePad, vi, emacs, SimpleText, or something else), create a file called HelloWorld.java (double-check your capitalization-Java is case-sensitive), and type the following contents into it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
};
Don't worry about the details of syntax right now; just type that in, save it, and exit your text editor. Make sure it's saved as a standard ASCII text file.
If you're on a UNIX or Windows machine, at the command (DOS) prompt, type the following:
HelloWorld.java
It should pause for a moment, then return you to your prompt.
Get a directory listing to make sure you have the following files:
>ls HelloWorld.class HelloWorld.java
If you get any errors, check the HelloWorld.java code to make sure it looks exactly as it does here.
If you get an error that javac was not found, you didn't set the JAVA/BIN directory in your PATH variable. Go back and check your installation.
Now you're ready to run your first Java program! At your command prompt, type the following:
HelloWorld
You should see the following:
Hello, World!
If you did, congratulations. You've run your first Java application, but more importantly, you've correctly and successfully installed the JDK.
If you didn't see "Hello, World!", there is something wrong with your installation. Check to make sure your CLASSPATH variable is set to point at both the current working directory (a period ".") and to the classes.zip file. Check to make sure you typed the name of the file correctly, keeping in mind that Java is case sensitive. If none of that works, you may need to reinstall the JDK.
The procedure is a bit different for a Macintosh because it doesn't have a command prompt.
1. On your Mac, open your HELLOWORLD folder so that your HelloWorld.java file appears.
2. Then open the MACJDK folder so that the java compiler icon appears (it should be a little "Duke" with a "C" on his chest). Drag the HelloWorld.java file onto the java compiler icon. The Java compiler then launches and begins compiling the program. When it's finished, a file called HelloWorld.class should appear in your HELLOWORLD folder.
3. If you received compile time errors, check the HelloWorld.java code to make sure it looks exactly as the previous code.
4. Double-click the HelloWorld.class file. The java runner application launches, loads the HelloWorld program, and runs theprogram. A window titled "stdout" should appear, with the words Hello, World! in them.
If it did, congrats. You've installed the JDK and run your first Java program.
If you didn't see "Hello, World!", there is something wrong with your installation. Check to make sure you are running System 7, that the JDK installed completely, and that the filename and the name of the class generated match, keeping in mind that Java is case sensitive. If you still can't get it to work, you may need to reinstall the JDK.
The authors of the Macintosh Java Runner application have cleverly hidden the Quit command in the Apple menu. Why they did that, I really don't know. If you want to free up the memory that the Java Runner is taking up after it's finished running your program, choose Apple, Java Runtime, Quit. Not very Mac-like, but at least it's not a command line.
Or, to quit, you can just hit command-q, like any other normal Mac program.
Now you're ready to take on the rest of this monster book. The next chapter goes into more detail regarding the various tools that come with the JDK. It's a pretty good place to go from here.